package com.dure.commonlib.util;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapRegionDecoder;
import android.graphics.Rect;
import android.text.TextUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;

/**
 * Created by leix on 15/10/2016.
 */

public class ImageUtil {

    public static Bitmap getBitmapFromUrl(String imageUrl) {
        return BitmapFactory.decodeStream(getInputStreamFromUrl(imageUrl, 1000));
    }

    public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis) {
        InputStream stream = null;
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            if (readTimeOutMillis > 0) {
                con.setReadTimeout(readTimeOutMillis);
            }
            stream = con.getInputStream();
        } catch (MalformedURLException e) {
//            IOUtils.close(stream);
            throw new RuntimeException("MalformedURLException occurred. ", e);
        } catch (IOException e) {
//            IOUtils.close(stream);
            throw new RuntimeException("IOException occurred. ", e);
        }
        return stream;
    }

    public static InputStream getInputStreamFromUrl(String imageUrl, int readTimeOutMillis,
                                                    Map<String, String> requestProperties) {
        InputStream stream = null;
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            setURLConnection(requestProperties, con);
            if (readTimeOutMillis > 0) {
                con.setReadTimeout(readTimeOutMillis);
            }
            stream = con.getInputStream();
        } catch (MalformedURLException e) {
//            IOUtils.close(stream);
            throw new RuntimeException("MalformedURLException occurred. ", e);
        } catch (IOException e) {
//            IOUtils.close(stream);
            throw new RuntimeException("IOException occurred. ", e);
        }
        return stream;
    }

    public static void setURLConnection(Map<String, String> requestProperties, HttpURLConnection urlConnection) {
        if (requestProperties == null || requestProperties.size() == 0 || urlConnection == null) {
            return;
        }

        for (Map.Entry<String, String> entry : requestProperties.entrySet()) {
            if (!TextUtils.isEmpty(entry.getKey())) {
                urlConnection.setRequestProperty(entry.getKey(), entry.getValue());
            }
        }
    }

    // 根据指定的区域来从图片中心展开获取一个相应比例的图片
    public static Bitmap getCutBitmap(InputStream is, float dstWidth, float dstHeight) {
        try {
            BitmapRegionDecoder bitmapRegionDecoder = BitmapRegionDecoder.newInstance(is, true);
            int bitmapWidth = bitmapRegionDecoder.getWidth();
            int bitmapHeight = bitmapRegionDecoder.getHeight();

            Rect srcRect = new Rect();

            float cutImageWidth, cutImageHeight;
            if (bitmapWidth > dstWidth && bitmapHeight > dstHeight) {
                cutImageWidth = dstWidth;
                cutImageHeight = dstHeight;
            } else if (dstWidth / dstHeight > bitmapWidth * 1.0f / bitmapHeight) {
                // 要放置图片区域的宽与高的比大于图片的宽高比
                // 如果以单位高来比较的话， 就是说图片的宽小于要展示的区域的宽
                // 因此以图片的宽基准来拉伸图片
                cutImageWidth = bitmapWidth;
                cutImageHeight = dstWidth / bitmapWidth * dstHeight;
            } else {
                cutImageHeight = bitmapHeight;
                cutImageWidth = dstHeight / bitmapHeight * dstWidth;
            }

            srcRect.left = (int) (bitmapWidth / 2 - cutImageWidth / 2);
            srcRect.top = (int) (bitmapHeight / 2 - cutImageHeight / 2);
            srcRect.right = (int) (bitmapWidth / 2 + cutImageWidth / 2);
            srcRect.bottom = (int) (bitmapHeight / 2 + cutImageHeight / 2);
            Bitmap bitmap = bitmapRegionDecoder.decodeRegion(srcRect, null);
            return bitmap;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
